package com.kinghunt.misp.service.impl.materialx;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.CommandResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.materialx.MaterialDTO;
import com.kinghunt.misp.common.dto.materialx.MaterialStockDTO;
import com.kinghunt.misp.common.dto.providex.ProvideDTO;
import com.kinghunt.misp.common.enums.materialx.MaterialTypeEnum;
import com.kinghunt.misp.common.param.materialx.MaterialParam;
import com.kinghunt.misp.common.param.materialx.MaterialStockParam;
import com.kinghunt.misp.dal.dao.materialx.MaterialDAO;
import com.kinghunt.misp.dal.dao.materialx.MaterialStockDAO;
import com.kinghunt.misp.dal.model.custx.CustDO;
import com.kinghunt.misp.dal.model.materialx.MaterialDO;
import com.kinghunt.misp.dal.model.materialx.MaterialStockDO;
import com.kinghunt.misp.service.api.materialx.MaterialService;
import com.kinghunt.misp.service.api.providex.ProvideService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class MaterialServiceImpl implements MaterialService {

    @Autowired(required = false)
    private MaterialDAO materialDAO;
    @Autowired(required = false)
    private MaterialStockDAO materialStockDAO;
    @Autowired(required = false)
    private ProvideService provideService;

    @Override
    public ApiResult<PageInfo<MaterialDTO>> getMaterialList(MaterialParam materialParam) {
        int offset = (materialParam.getPageNum() - 1) * materialParam.getPageSize();
        MaterialDO materialDO = ConvertUtil.convert(materialParam, MaterialDO.class);
        int count = materialDAO.selectMaterialListCount(materialDO);
        PageInfo<MaterialDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(materialParam.getPageNum());
        pageInfo.setPageSize(materialParam.getPageSize());
        List<MaterialDO> materialDOList = materialDAO.selectMaterialList(materialDO, offset, materialParam.getPageSize());
        List<MaterialDTO> dto =  ConvertUtil.convert(materialDOList, MaterialDTO.class);
        List<Long> provideIds = dto.stream().map(MaterialDTO::getProvideId).collect(Collectors.toList());
        ApiResult<List<ProvideDTO>> apiResult =  provideService.getProvideByIds(provideIds);
        if(!apiResult.isSuccess()){
            return ApiResult.failOfMessage("您供货商不存在！ ");
        }
        dto.stream().map(material -> apiResult.getData().stream()
                .filter(provide -> Objects.equals(provide.getProvideId() , material.getProvideId()))
                .findFirst().map(provide -> {
                    if(null == provide){
                        return ApiResult.failOfMessage("您供货商不存在！ ");
                    }
                    material.setProvideName(provide.getProvideName());
                    return material;
                }).orElse(null)
        ).collect(Collectors.toList());
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<PageInfo<MaterialStockDTO>> getMaterialStockList(MaterialParam materialParam) {
        int offset = (materialParam.getPageNum() - 1) * materialParam.getPageSize();
        MaterialStockDO materialStockDO = ConvertUtil.convert(materialParam, MaterialStockDO.class);
        int count = materialStockDAO.selectMaterialStockListCount(materialStockDO);
        PageInfo<MaterialStockDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(materialParam.getPageNum());
        pageInfo.setPageSize(materialParam.getPageSize());
        List<MaterialStockDO> materialDOList = materialStockDAO.selectMaterialStockList(materialStockDO, offset, materialParam.getPageSize());
        List<MaterialStockDTO> dto =  ConvertUtil.convert(materialDOList, MaterialStockDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<MaterialDTO> getMaterialDetail(Long materialId) {
        MaterialDO materialDO = materialDAO.selectByPrimaryKey(materialId);
        MaterialDTO materialDTO = ConvertUtil.convert(materialDO, MaterialDTO.class);
        return ApiResult.ok(materialDTO);
    }

    @Override
    public ApiResult addMaterial(MaterialParam materialParam) {
        //判断耗材编号是否唯一
        List<MaterialDO> materialDOList = materialDAO.selectByMaterialNo(materialParam.getMaterialNo());
        if(materialDOList != null && materialDOList.size() > 0){
            return ApiResult.failOfMessage("您新增的耗材编号已存在！ ");
        }
        MaterialDO materialDO = ConvertUtil.convert(materialParam, MaterialDO.class);
        // 保存地址到数据库
        materialDAO.insert(materialDO);
        MaterialStockDO materialStockDO = new MaterialStockDO();
        materialStockDO.setMaterialId(materialDO.getMaterialId());
        materialStockDO.setStockType(MaterialTypeEnum.MATERIAL_TYPE_2.getValue());
        materialStockDO.setQuantityChange(materialParam.getQuantity());
        materialStockDO.setStockUserId(materialParam.getCreatorId());
        materialStockDO.setRemark("初始化入库");
        materialStockDAO.insert(materialStockDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateMaterial(MaterialParam materialParam) {
        MaterialDO materialDO = materialDAO.selectByPrimaryKey(materialParam.getMaterialId());
        if(null == materialDO){
            return ApiResult.failOfMessage("您修改的耗材不存在！ ");
        }
        MaterialDO updateMaterialDO = ConvertUtil.convert(materialParam, MaterialDO.class);
        materialDAO.updateByPrimaryKeySelective(updateMaterialDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteMaterial(Long materialId) {
        MaterialDO materialDO = materialDAO.selectByPrimaryKey(materialId);
        if(null == materialDO){
            return ApiResult.failOfMessage("您删除的耗材不存在！ ");
        }
        materialDAO.deleteByPrimaryKey(materialId);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateMaterialStock(MaterialStockParam materialStockParam) {
        //更新耗材表里面的数量
        MaterialDO materialDO = materialDAO.selectByPrimaryKey(materialStockParam.getMaterialId());
        if(null == materialDO){
            return ApiResult.failOfMessage("您修改的耗材不存在！ ");
        }
        MaterialDO updateMaterialDO =  new MaterialDO();
        updateMaterialDO.setMaterialId(materialDO.getMaterialId());
        updateMaterialDO.setQuantity(materialDO.getQuantity() - materialStockParam.getQuantityChange());
        materialDAO.updateByPrimaryKeySelective(updateMaterialDO);
        //新增耗材出入库明细
        MaterialStockDO materialStockDO = ConvertUtil.convert(materialStockParam, MaterialStockDO.class);
        materialStockDAO.insert(materialStockDO);
        return ApiResult.ok();
    }
}
